﻿using Cognitum.CassandraProviders.Cassandra;
using NUnit.Framework;
using System;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace Cognitum.ProvidersTests
{
    [TestFixture]
    internal class MembershipTests : BaseProvidersTest
    {
        #region | Tests |

        #region | ChangePassword |

        [Test]
        public void ChangePassword_EmptyDatabase()
        {
            // Arrange
            string newPassword = "newPassword";
            // Act
            bool result2 = _providerMembership.ChangePassword("userdName15", _password, newPassword);
            MembershipUser result = _providerMembership.GetUser(_userName, false);
            // Assert
            Assert.IsNull(result);
            Assert.IsFalse(result2);
        }

        [Test]
        public void ChangePassword_IsOnDatabase()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string newPassword = "newPassword";
            // Act
            bool result2 = _providerMembership.ChangePassword(_userName, _password, newPassword);
            string result = _providerMembership.GetPassword(_userName, _passwordAnswer);
            //Assert
            Assert.AreEqual(newPassword, result);
            Assert.IsTrue(result2);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ChangePassword_IsOnDatabase_WrongPassword()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string newPassword = "newPassword";
            // Act
            bool result = _providerMembership.ChangePassword(_userName, "pasword", newPassword);
            //Assert
            Assert.IsFalse(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | ChangePassword |

        #region | ChangePasswordQuestionAndAnswer |

        [Test]
        public void ChangePasswordQuestionAndAnswer_EmptyDatabase()
        {
            // Arrange
            string newPasswordAnswer = "newPasswordAnswer";
            string newPasswordQuestion = "newPasswordQuestion";
            // Act
            bool result = _providerMembership.ChangePasswordQuestionAndAnswer(_userName, _password, newPasswordQuestion, newPasswordAnswer);
            MembershipUser user = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsFalse(result);
            Assert.IsNull(user);
        }

        [Test]
        public void ChangePasswordQuestionAndAnswer_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string newPasswordAnswer = "newPasswordAnswer";
            string newPasswordQuestion = "newPasswordQuestion";
            // Act
            bool result = _providerMembership.ChangePasswordQuestionAndAnswer(_userName, _password, newPasswordQuestion, newPasswordAnswer);
            MembershipUser user = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsTrue(result);
            Assert.AreEqual(newPasswordQuestion, user.PasswordQuestion);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ChangePasswordQuestionAndAnswer_WrongPassword()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string newPasswordAnswer = "newPasswordAnswer";
            string newPasswordQuestion = "newPasswordQuestion";
            // Act
            bool result = _providerMembership.ChangePasswordQuestionAndAnswer(_userName, "assword", newPasswordQuestion, newPasswordAnswer);
            MembershipUser user = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsFalse(result);
            Assert.AreNotEqual(newPasswordQuestion, user.PasswordQuestion);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | ChangePasswordQuestionAndAnswer |

        #region | CreateUser |

        [Test]
        public void CreateUser_HappyPath()
        {
            int NumberOfUsersOnDatabase2;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase2);
            // Arrange
            //Act
            MembershipUser result = _providerMembership.CreateUser(
              _userName,
              _password,
              _email,
              _passwordQuestion,
              _passwordAnswer,
              _isApproved,
              _providerUserKey,
              out _status
              );
            MembershipUser result2 = _providerMembership.GetUser(_userName, false);
            int NumberOfUsersOnDatabase = 0;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase);
            // Assert
            Assert.AreEqual(_userName, result.UserName);
            Assert.AreEqual(_userName, result2.UserName);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 1, NumberOfUsersOnDatabase);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void CreateUser_ThreeWithTheSameEmail_RequiresUniqueEmailIsFalse_AddedThree()
        {
            _providerMembership = new CassandraMembershipProvider();
            _conf["requiresUniqueEmail"] = "false";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            int NumberOfUsersOnDatabase2;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase2);
            // Arrange
            // Act
            _providerMembership.CreateUser(
                _userName,
                _password,
                _email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );
            _providerMembership.CreateUser(
                "userdName",
                _password,
                _email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );
            _providerMembership.CreateUser(
                "userdName2",
                "password",
                "emai",
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
               "da",
                out _status
                );

            MembershipUser result = _providerMembership.GetUser(_userName, false);
            int NumberOfUsersOnDatabase = 0;
            MembershipUserCollection users = _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase);
            // Assert
            Assert.AreEqual(_userName, result.UserName);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 3, NumberOfUsersOnDatabase);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 3 > 100 ? 100 : NumberOfUsersOnDatabase2 + 3, users.Count);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void CreateUser_ThreeWithTheSameEmail_RequiresUniqueEmailIsTrue_AddedOne()
        {
            _providerMembership = new CassandraMembershipProvider();
            _conf["requiresUniqueEmail"] = "true";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);

            int NumberOfUsersOnDatabase2;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase2);

            // Arrange Act
            _providerMembership.CreateUser(
                _userName,
                _password,
                _email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );
            _providerMembership.CreateUser(
                "userdName",
                _password,
                _email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );
            _providerMembership.CreateUser(
                "userdName1",
                _password,
                _email,
                _passwordQuestion,
                _passwordAnswer,
                _isApproved,
                _providerUserKey,
                out _status
                );

            MembershipUser result = _providerMembership.GetUser(_userName, false);
            int NumberOfUsersOnDatabase = 0;
            MembershipUserCollection users = _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase);
            // Assert
            Assert.AreEqual(_userName, result.UserName);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 1, NumberOfUsersOnDatabase);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 1 > 100 ? 100 : NumberOfUsersOnDatabase2 + 1, users.Count);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void CreateUser_WithTheSameUsernameOnOneApplication_AddedOne()
        {
            // Arrange
            //Act
            int NumberOfUsersOnDatabase2;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase2);
            MembershipUser result = _providerMembership.CreateUser(
              _userName,
              _password,
              _email,
              _passwordQuestion,
              _passwordAnswer,
              _isApproved,
              _providerUserKey,
              out _status
              );
            MembershipUser result2 = _providerMembership.CreateUser(
           _userName,
           _password,
           _email,
           "passwordQuestionAnother",
           _passwordAnswer,
           _isApproved,
           _providerUserKey,
           out _status
           );
            MembershipUser result3 = _providerMembership.CreateUser(
           _userName,
           _password,
           _email,
            "passwordQuestionAnother",
           _passwordAnswer,
           _isApproved,
           _providerUserKey,
           out _status
           );

            MembershipUser result4 = _providerMembership.GetUser(_userName, false);
            int NumberOfUsersOnDatabase = 0;
            _providerMembership.GetAllUsers(0, 100, out NumberOfUsersOnDatabase);
            // Assert
            Assert.AreEqual(_passwordQuestion, result.PasswordQuestion);
            Assert.AreEqual(_passwordQuestion, result4.PasswordQuestion);
            Assert.AreEqual(NumberOfUsersOnDatabase2 + 1, NumberOfUsersOnDatabase);
            Assert.IsNull(result2);
            Assert.IsNull(result3);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | CreateUser |

        #region | DeleteUser |

        [Test]
        public void DeleteUser_EmptyDatabase()
        {
            // Arrange Act
            _providerMembership.DeleteUser(_userName, true);
            MembershipUser result = _providerMembership.GetUser(_userName, false);
            // Assert
            Assert.IsNull(result);
        }

        [Test]
        public void DeleteUser_EmptyOnDatabaseByWrongUsername()
        {
            // Arrange Act
            _providerMembership.DeleteUser(_userNameWrong, true);
            MembershipUser result = _providerMembership.GetUser(_userName, false);
            // Assert
            Assert.IsNull(result);
        }

        [Test]
        public void DeleteUser_IsOnDatabase()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            _providerMembership.DeleteUser(_userName, true);
            MembershipUser result = _providerMembership.GetUser(_userName, false);
            // Assert
            Assert.IsNull(result);
        }

        [Test]
        public void DeleteUser_IsOnDatabase_NotDeletePhisically_IsOnRole()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string writer = "writer";
            _providerRole.CreateRole(writer);
            _providerRole.AddUsersToRoles(new string[] { _userName }, new string[] { writer });
            // Act
            _providerMembership.DeleteUser(_userName, false);
            MembershipUser result = _providerMembership.GetUser(_userName, true);
            string[] roles = _providerRole.GetRolesForUser(_userName);
            // Assert
            Assert.NotNull(result);
            Assert.AreEqual(writer, roles[0]);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            roles = _providerRole.GetRolesForUser(_userName);
            _providerRole.DeleteRole(writer, false);
            result = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsNull(result);
            Assert.IsEmpty(roles);
        }

        [Test]
        public void DeleteUser_IsOnDatabaseNotDeletePhisically()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            _providerMembership.DeleteUser(_userName, false);
            MembershipUser result = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.NotNull(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void DeleteUser_NotEmptyDatabaseAndWrongUserName()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            _providerMembership.DeleteUser(_userNameWrong, true);
            MembershipUser resultDeleted = _providerMembership.GetUser(_userNameWrong, false);
            MembershipUser result = _providerMembership.GetUser(_userName, false);
            // Assert
            Assert.IsNull(resultDeleted);
            Assert.IsNotNull(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | DeleteUser |

        #region | FindUsersByEmail |

        [Test]
        public void FindUsersByEmail_EmptyDatabase()
        {
            // Arrange
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByEmail(_email, 0, 10, out totalRecords);

            // Assert
            Assert.IsEmpty(user);
        }

        [Test]
        public void FindUsersByEmail_FewOnDatabaseFewMatch()
        {
            int totalRecords2;
            _providerMembership.FindUsersByEmail("email", 0, 10, out totalRecords2);
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");

            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByEmail("email", 0, 2, out totalRecords);

            // Assert
            Assert.IsNotEmpty(user);
            Assert.AreEqual(4 + totalRecords2, totalRecords);
            Assert.AreEqual(2, user.Count);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void FindUsersByEmail_FewOnDatabaseOneMatch()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByEmail(_email, 0, 10, out totalRecords);

            // Assert
            Assert.AreEqual(1, user.Count);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void FindUsersByEmail_OneOnDatabase()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByEmail(_email, 0, 10, out totalRecords);

            // Assert
            Assert.AreEqual(1, user.Count);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | FindUsersByEmail |

        #region | FindUsersByName |

        [Test]
        public void FindUsersByName_EmptyDatabase()
        {
            // Arrange
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByName("pl", 0, 10, out totalRecords);

            // Assert
            Assert.IsEmpty(user);
        }

        [Test]
        public void FindUsersByName_FewOnDatabaseFewMatch()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByName("serdNam", 0, 10, out totalRecords);

            // Assert
            Assert.AreEqual(3, user.Count);
            Assert.AreEqual(3, totalRecords);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void FindUsersByName_FewOnDatabaseFewMatch_Paging()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act

            MembershipUserCollection user = _providerMembership.FindUsersByName("serdNam", 1, 2, out totalRecords);

            // Assert
            Assert.AreEqual(1, user.Count);
            Assert.AreEqual(3, totalRecords);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void FindUsersByName_FewOnDatabaseNoneMatch()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByName("pl", 0, 10, out totalRecords);

            // Assert
            Assert.IsEmpty(user);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void FindUsersByName_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act
            MembershipUserCollection user = _providerMembership.FindUsersByName(_userName, 0, 10, out totalRecords);

            // Assert
            Assert.AreEqual(1, user.Count);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        #endregion | FindUsersByName |

        #region | GetAllUsers |

        [Test]
        public void GetAllUsers_NothingAddedToDatabase()
        {
            int totalRecords;
            // Arrange
            // Act
            MembershipUserCollection result = _providerMembership.GetAllUsers(0, 100, out totalRecords);
            // Assert
            Assert.AreEqual(result.Count, totalRecords > 100 ? 100 : totalRecords);
        }

        [Test]
        public void GetAllUsers_HappyPath()
        {
            int totalRecords2;
            _providerMembership.GetAllUsers(0, 100, out totalRecords2);
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            int totalRecords;
            // Act
            MembershipUserCollection result = _providerMembership.GetAllUsers(0, 100, out totalRecords);
            // Assert
            Assert.AreEqual(totalRecords2 + 4 > 100 ? 100 : totalRecords2 + 4, result.Count);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        #endregion | GetAllUsers |

        #region | ApplicationName |

        [Test]
        public void GetApplicationName_HappyPath()
        {
            Assert.AreSame(_applicationName, _providerMembership.ApplicationName);
        }

        #endregion | ApplicationName |

        #region | GetNumberOfUsersOnline |

        [Test]
        public void GetNumberOfUsersOnline_NothingAddedDatabase()
        {
            int result2 = _providerMembership.GetNumberOfUsersOnline();
            // Arrange
            // Act
            int result = _providerMembership.GetNumberOfUsersOnline();
            // Assert
            Assert.IsTrue(result <= result2);
            //Clean
        }

        [Test]
        public void GetNumberOfUsersOnline_FewUsers()
        {
            int result2 = _providerMembership.GetNumberOfUsersOnline();
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "aa@ad.pl");
            AddDefaultUser("userdName2", "aa2@ad.pl");
            AddDefaultUser("userdName3", "aa3@ad.pl");
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            // Act
            int result = _providerMembership.GetNumberOfUsersOnline();
            // Assert
            Assert.AreEqual(result2 + 4, result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName2", true);
            _providerMembership.DeleteUser("userdName3", true);
        }

        [Test]
        public void GetNumberOfUsersOnline_HappyPath()
        {
            int result2 = _providerMembership.GetNumberOfUsersOnline();
            // Arrange
            AddDefaultUser(_userName, _email);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            _providerMembership.ResetPassword(_userName, _passwordAnswer);
            // Act
            int result = _providerMembership.GetNumberOfUsersOnline();
            // Assert
            Assert.AreEqual(result2 + 1, result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | GetNumberOfUsersOnline |

        #region | GetPassword |

        [Test]
        public void GetPassword_NotOnDatabase()
        {
            // Arrange Act Assert
            Assert.Throws(
                typeof(MembershipPasswordException),
                delegate
                {
                    _providerMembership.GetPassword(_userName, _passwordAnswer);
                }
            );
        }

        [Test]
        public void GetPassword_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            String result = _providerMembership.GetPassword(_userName, _passwordAnswer);
            // Assert
            Assert.AreEqual(_password, result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetPassword_NotEnablePasswordRetrieval()
        {
            // Arrange
            _providerMembership = new CassandraMembershipProvider();
            _conf["enablePasswordRetrieval"] = "false";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            AddDefaultUser(_userName, _email);
            // Act Assert
            Assert.Throws(
                typeof(ProviderException),
                delegate
                {
                    _providerMembership.GetPassword(_userName, _passwordAnswer);
                }
            );
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetPassword_WrongPasswordAnswer_NotRequiresQuestionAndAnswer()
        {
            // Arrange
            _providerMembership = new CassandraMembershipProvider();
            _conf["requiresQuestionAndAnswer"] = "false";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            AddDefaultUser(_userName, _email);
            // Act
            String result = _providerMembership.GetPassword(_userName, _passwordAnswer);
            // Assert
            Assert.AreEqual(_password, result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetPassword_WrongPasswordAnswer_RequiresQuestionAndAnswer()
        {
            // Arrange
            _providerMembership = new CassandraMembershipProvider();
            _conf["requiresQuestionAndAnswer"] = "true";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            AddDefaultUser(_userName, _email);
            // Act Assert
            Assert.Throws(
                typeof(MembershipPasswordException),
                delegate
                {
                    _providerMembership.GetPassword(_userName, "passwordAnswerWrong");
                }
            );
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | GetPassword |

        #region | GetUser |

        [Test]
        public void GetUserByProviderUserKey_EmptyDatabase()
        {
            // Arrange Act
            MembershipUser result = _providerMembership.GetUser(_providerUserKey, true);
            // Assert
            Assert.IsNull(result);
        }

        [Test]
        public void GetUserByProviderUserKey_HappyPath()
        {
            // Arrange
            Guid provKey = GetPartitionKeyFromUsername(_userName);
            AddDefaultUser(_userName, _email);
            // Act
            MembershipUser result = _providerMembership.GetUser(provKey, true);
            // Assert
            Assert.AreEqual(_userName, result.UserName);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetUserByProviderUserKey_WrongProviderKey()
        {
            // Arrange
            Guid provKey = GetPartitionKeyFromUsername("userdName");
            AddDefaultUser(_userName, _email);
            // Act
            MembershipUser result = _providerMembership.GetUser(provKey, true);
            // Assert
            Assert.IsNull(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetUserByUsername_NotOnDatabase()
        {
            // Arrange Act
            MembershipUser result = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsNull(result);
        }

        [Test]
        public void GetUserByUsername_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            MembershipUser result = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.AreEqual(_userName, result.UserName);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void GetUserByUsername_WrongUserName()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            MembershipUser result = _providerMembership.GetUser("userdName", true);
            // Assert
            Assert.IsNull(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | GetUser |

        #region | GetUserNameByEmail |

        [Test]
        public void GetUserNameByEmail_EmptyDatabase()
        {
            // Arrange Act
            string result = _providerMembership.GetUserNameByEmail(_email);
            // Assert
            Assert.IsEmpty(result);
        }

        [Test]
        public void GetUserNameByEmail_FewInDatabaseOneMatch()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            // Act
            string result = _providerMembership.GetUserNameByEmail("email@a.pl");

            // Assert
            Assert.AreEqual("userdname", result);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void GetUserNameByEmail_FewInDatabaseNoneMatch()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            // Act
            string result = _providerMembership.GetUserNameByEmail("pl");

            // Assert
            Assert.AreEqual(String.Empty, result);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        [Test]
        public void GetUserNameByEmail_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", "email@a.pl");
            AddDefaultUser("userdName1", "email1@a.pl");
            AddDefaultUser("userdName2", "email2@a.pl");
            // Act
            string result = _providerMembership.GetUserNameByEmail(_email);

            // Assert
            Assert.AreEqual(_userName, result);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName1", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        #endregion | GetUserNameByEmail |

        #region | ResetPassword |

        [Test]
        public void ResetPassword_EmptyDatabase()
        {
            // Arrange Act Assert
            Assert.Throws(
                typeof(MembershipPasswordException),
                delegate { _providerMembership.ResetPassword(_userName, _passwordAnswer); }
                );
        }

        [Test]
        public void ResetPassword_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            String result = _providerMembership.ResetPassword(_userName, _passwordAnswer);
            string resultPassword = _providerMembership.GetPassword(_userName, _passwordAnswer);
            // Assert
            Assert.AreEqual(result, resultPassword);
            Assert.AreNotEqual(_password, result);
            Assert.AreNotEqual(_password, resultPassword);
            Assert.IsNotEmpty(result);

            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ResetPassword_WrongAnswer_NotRequiresQuestionAndAnswer()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            String result = _providerMembership.ResetPassword(_userName, "passwordWrongAnswer");
            // Assert
            Assert.IsNotEmpty(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ResetPassword_WrongAnswer_RequiresQuestionAndAnswer()
        {
            // Arrange
            _providerMembership = new CassandraMembershipProvider();
            _conf["requiresQuestionAndAnswer"] = "true";
            _providerMembership.Initialize("CustomMembershipProvider", _conf);
            AddDefaultUser(_userName, _email);
            // Act Assert
            Assert.Throws(typeof(MembershipPasswordException),
                delegate { _providerMembership.ResetPassword(_userName, "passwordWrongAnswer"); }
            );
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ResetPassword_WrongUsername()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act Assert
            Assert.Throws(
                typeof(MembershipPasswordException),
                delegate { _providerMembership.ResetPassword(_userNameWrong, _passwordAnswer); }
            );
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | ResetPassword |

        #region | UnlockUser |

        [Test]
        public void UnlockUser_EmptyDatabase()
        {
            // Arrange Act
            bool result = _providerMembership.UnlockUser(_userName);
            MembershipUser resultUser = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsFalse(result);
            Assert.IsNull(resultUser);
        }

        [Test]
        public void UnlockUser_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            bool result = _providerMembership.UnlockUser(_userName);
            MembershipUser resultUser = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsTrue(result);
            Assert.IsFalse(resultUser.IsLockedOut);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void UnlockUser_WrongUserName()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            MembershipUser resultUser = _providerMembership.GetUser(_userName, true);
            bool result = _providerMembership.UnlockUser("userdName");
            MembershipUser resultUser1 = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.IsFalse(result);
            Assert.IsNotNull(resultUser);
            Assert.IsNotNull(resultUser1);
            Assert.AreEqual(resultUser1.IsLockedOut, resultUser.IsLockedOut);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | UnlockUser |

        #region | UpdateUser |

        [Test]
        public void UpdateUser_EmptyDatabase()
        {
            // Arrange
            MembershipUser user = new MembershipUser(
                    "CustomMembershipProvider",
                    _userName,
                   GetPartitionKeyFromUsername(_userName),
                  _email,
                  _passwordQuestion,
                 String.Empty,
                 _isApproved,
                 false,
                  DateTime.Now,
                   DateTime.Now,
                    DateTime.Now,
                     DateTime.Now,
                      DateTime.Now
                    );
            // Act Assert
            Assert.Throws(typeof(MembershipPasswordException),
                delegate { _providerMembership.UpdateUser(user); }
                );
        }

        [Test]
        public void UpdateUser_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            string comment = "comment";
            MembershipUser user = new MembershipUser(
                    "CustomMembershipProvider",
                    _userName,
                    GetPartitionKeyFromUsername(_userName),
                  _email,
                  _passwordQuestion,
                 comment,
                 _isApproved,
                 false,
                  DateTime.Now,
                   DateTime.Now,
                    DateTime.Now,
                     DateTime.Now,
                      DateTime.Now
                    );
            // Act
            _providerMembership.UpdateUser(user);
            MembershipUser result = _providerMembership.GetUser(_userName, true);
            // Assert
            Assert.AreEqual(comment, result.Comment);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        #endregion | UpdateUser |

        #region | ValidateUser |

        [Test]
        public void ValidateUser_HappyPath()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            bool result = _providerMembership.ValidateUser(_userName, _password);
            // Assert
            Assert.IsTrue(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ValidateUser_EmptyDatabase()
        {
            // Arrange
            // Act
            bool result = _providerMembership.ValidateUser(_userName, _password);
            // Assert
            Assert.IsFalse(result);
        }

        [Test]
        public void ValidateUser_WrongPassword()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            // Act
            bool result = _providerMembership.ValidateUser(_userName, "pass");
            // Assert
            Assert.IsFalse(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
        }

        [Test]
        public void ValidateUser_WrongUsername()
        {
            // Arrange
            AddDefaultUser(_userName, _email);
            AddDefaultUser("userdName", _email);
            AddDefaultUser("userdName2", _email);
            // Act
            bool result = _providerMembership.ValidateUser("use", _password);
            // Assert
            Assert.IsFalse(result);
            //Clean
            _providerMembership.DeleteUser(_userName, true);
            _providerMembership.DeleteUser("userdName", true);
            _providerMembership.DeleteUser("userdName2", true);
        }

        #endregion | ValidateUser |

        #endregion | Tests |

        #region Methods

        private Guid GetPartitionKeyFromUsername(string username)
        {
            UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
            byte[] message = unicodeEncoding.GetBytes(username);
            MD5 hashString = new MD5CryptoServiceProvider();
            return new Guid(hashString.ComputeHash(message));
        }

        #endregion Methods
    }
}